'use strict';

exports.main = async (event, context) => {
  try {
    const db = uniCloud.database();
    const loginCollection = db.collection('login');
    
    // 检查是否已有数据
    const { total } = await loginCollection.count();
    if (total > 0) {
      return {
        success: false,
        message: '数据库中已有数据，请勿重复初始化'
      };
    }

    // 添加测试数据
    const testData = [
      {
        id: 'test001',
        lastLoginTime: null,
        create_date: new Date()
      },
      {
        id: 'test002',
        lastLoginTime: null,
        create_date: new Date()
      }
    ];

    const result = await loginCollection.add(testData);
    
    return {
      success: true,
      message: '初始化数据成功',
      data: result
    };
  } catch (error) {
    console.error('初始化数据失败:', error);
    return {
      success: false,
      message: '初始化数据失败',
      error: error.message
    };
  }
}; 